define(function(require){
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");
	require("../../lib/3rdlib/baidu/echarts/dist/echarts-all");
	
	var Model = function(){
		this.callParent();
	};

	Model.prototype.windowReceiverRealDataReceive = function(event){
		this.pointId = event.data.pointID;
		this.table = event.data.table;
		this.pointName = event.data.pointName;
		this.pointUnit = typeof(event.data.pointUnit) != "undefined" ? event.data.pointUnit : "";
		debugger;
		this.drawCurve(this.pointId,this.pointName + "(" + this.pointUnit + ")",this.comp("buttonGroup1").selected);
	};

	Model.prototype.buttonBackClick = function(event){
		this.close();
	};

	Model.prototype.getFilter = function(id, fromDate, toDate) {
		if (typeof (fromDate) == "undefined" || typeof (toDate) == "undefined" /*|| typeof (fromDate.value) == "undefined" || typeof (toDate.value) == "undefined"*/) {
			return "1=0";
		} else {
			fromDate.setHours(0, 0, 0, 0);
			toDate.setHours(23, 59, 59, 999);
		}
		var filter = "fPointID = '" + id + "' and fDate >= '" + justep.Date.toString(fromDate, justep.Date.STANDART_FORMAT_SHOT) + "' and fDate <= '" + justep.Date.toString(toDate, justep.Date.STANDART_FORMAT_SHOT) + "'";
		// debugger;
		return filter;
	};
	
	Model.prototype.drawCurve = function(pointId, legend, selectTime){	
		if(typeof(pointId)=="undefined" || typeof(legend)=="undefined"){
			justep.Util.hint("数据点无效");
			return;
		}
		var hisData = this.comp("hisData");
		var now = new Date();
		now.setHours(23, 59, 59, 999);
		var yesterday = new Date();
		switch(selectTime)
		{
			case "button3Month":
				yesterday.setMonth(yesterday.getMonth() - 3);
			break;
			case "buttonMonth":
				yesterday.setMonth(yesterday.getMonth() - 1);
			break;
			case "buttonWeek":
				yesterday.setDate(yesterday.getDate() - 7);
			break;
			default:
				yesterday.setDate(yesterday.getDate() - 7);
			break;
		}
		yesterday.setHours(0, 0, 0, 0);
		hisData.setFilter('filter0', this.getFilter(pointId, yesterday, now));
		hisData.refreshData();
		var dateList = [], valueList = [];
		hisData.each(function(param){
			dateList.push(param.row.val('fDate'));
			valueList.push(param.row.val('fValue'));
	    });
        var option = {
            tooltip : {
                trigger: 'axis'
            },
            legend: {
                data:[legend]
            },
            grid:{ 
            	x:25, y:45, x2:5, y2:20
            },
            toolbox: {
                show : false,
                feature : {
                    mark : {show: true},
                    dataView : {show: true, readOnly: false},
                    magicType : {show: true, type: ['line', 'bar']},
                    restore : {show: true},
                    saveAsImage : {show: true}
                }
            },
            calculable : true,
            xAxis : [
                {
                    type : 'category',
                    data : dateList
                }
            ],
            yAxis : [
                {
                    type : 'value',
                    splitArea : {show : true}
                }
            ],
            series : [
                {
                    name:legend,
                    type:'line',
                    data:valueList
                }
            ]
        };
        var myChart = echarts.init(this.getElementByXid('content1'));
        myChart.setOption(option);
    };
	Model.prototype.buttonGroup1Select = function(event){
		if(typeof(this.pointId)=="undefined" || typeof(this.pointName)=="undefined"){
			justep.Util.hint("数据点无效");
			return;
		}
		this.drawCurve(this.pointId,this.pointName + "(" + this.pointUnit + ")", event.item.getXid());
	};
	return Model;
});